//
//  LoginViewController.swift
//  HinsLive
//
//  Created by haoxian on 2019/4/20.
//  Copyright © 2019 Hins. All rights reserved.
//

import UIKit
import RxSwift
import RxCocoa
import Toast
import SVProgressHUD


class LoginViewController: ViewController {
    
    @IBOutlet weak var accountTF: UITextField! {
        didSet {
            guard let account = UserDefaults.standard.value(forKey: AccountManager.kUserAccountKey) as? String else { return }
            accountTF.text = account
            accountTF.rx.text.orEmpty.changed.map { $0.count <= 50 }
                .subscribe(onNext: { [weak self] enable in
                    if enable { return }
                    self?.accountTF.deleteBackward()
                })
                .disposed(by: bag)
        }
    }
    @IBOutlet weak var passwordTF: UITextField! {
        didSet {
            passwordTF.rx.text.orEmpty.changed.map { $0.count <= 100 }
                .subscribe(onNext: { [weak self] enable in
                    if enable { return }
                    self?.passwordTF.deleteBackward()
                })
                .disposed(by: bag)
        }
    }
    @IBOutlet weak var loginBtn: UIButton! {
        didSet {
            loginBtn.layer.cornerRadius = 3
            loginBtn.layer.masksToBounds = true
        }
    }
    @IBOutlet weak var registerBtn: UIButton! {
        didSet {
            registerBtn.layer.cornerRadius = 3
            registerBtn.layer.masksToBounds = true
            registerBtn.rx.tap.subscribe(onNext: { [weak self] _ in
                self?.view.endEditing(true)
                self?.navigationController?
                    .pushViewController(RegisterViewController(nib: R.nib.registerViewController), animated: true)
            }).disposed(by: bag)
        }
    }
    
    private lazy var viewModel = LoginViewModel(accountDriver: accountTF.rx.text.orEmpty.asDriver(),
                                                passwordDriver: passwordTF.rx.text.orEmpty.asDriver())

    override func viewDidLoad() {
        super.viewDidLoad()
        
        viewModel.loginEnable.drive(loginBtn.rx.isEnableTap).disposed(by: bag)
        
        loginBtn.rx.tap
            .map { [weak self] _ in
                self?.view.endEditing(true)
                SVProgressHUD.show()
            }
            .withLatestFrom(Observable.combineLatest(accountTF.rx.text.orEmpty.asObservable(),
                                                     passwordTF.rx.text.orEmpty.asObservable(),
                                                     resultSelector: { ($0, $1) }))
            .flatMap(viewModel.loginTap)
            .do(onNext: { _ in
                SVProgressHUD.dismiss()
            })
            .subscribe(onNext: { [weak self] _ in
                self?.navigationController?.dismiss(animated: true)
            }, onError: { [weak self] (error) in
                self?.view.makeCenterMsg(error.localizedDescription)
            })
            .disposed(by: bag)
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillAppear(animated)
        view.endEditing(true)
    }

    @IBAction func vistorLogin(_ sender: UIButton) {
        navigationController?.dismiss(animated: true)
    }
}


